package com.edu.controller;

import com.edu.pojo.Member;
import com.edu.pojo.users;
import com.edu.service.CollegeService;
import com.edu.service.DepartmentService;
import com.edu.service.MemberService;
import com.edu.service.userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * adminLogin() 主席登陆
 * teacherLogin() 部长登陆
 * studentLogin() 成员登陆
 * quitSystem() 退出系统
 * modifyPassword() 修改密码，用一个controller作为修改。
 * 修改了teacherLogin() 方法，新增了查询教师信息的功能。并且写入session中。
 *
 */

@Controller
@RequestMapping("/Users")
public class usercontroller {
    @Autowired
    @Qualifier("userserviceImpl")
    private userservice userservice;
    @Autowired
    private MemberService memberService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private CollegeService collegeService;

    @RequestMapping("/allUser")
    public String list(Model model){
        List<users> users = userservice.queryAlluser();
        model.addAttribute("list",users);
        return "alluser";
    }
    @RequestMapping("/toAddPager")
    public String  toAddPager(){
        return "adduser";
    }
    @RequestMapping("/adduser")
    public String adduser(users users){
        userservice.adduser(users);
        return "redirect:/Users/allUser";
    }
    @RequestMapping("/toUpdatePager")
    public String toUpdatePager(String id,Model model){
        users users = userservice.queryuserById(id);
        System.out.println(users);
        model.addAttribute("QBuser",users);
        return "Updateuser";
    }
    @RequestMapping ("/updateuser")
    public String updateuser(users users){
        System.out.println(users);
        userservice.updateuser(users);
        return "redirect:/Users/allUser";
    }
    @RequestMapping ("/deleteuser")
    public String deleteuser(String id){
        userservice.deleteusersById(id);
        return "redirect:/Users/allUser";
    }
    @RequestMapping(value="/Chair/login")
    public String ChairLogin(String accounts, String password, Model model, HttpServletRequest request) {
//        System.out.println(accounts);
//        System.out.println(password);
        users user = userservice.login(accounts, password);
        if(user==null) {
            return "redirect:ToChairLogin";
        }
        if(user.getPermission()=='3') {
            System.out.println("传入");
            System.out.println(user);
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(3600);
            session.setAttribute("user", user);
            return "Chair/main";
        }else {
            return "redirect:ToChairLogin";
        }
    }
    @RequestMapping(value="/Chair/ToChairLogin")
    public String ToChairLogin(Model model,HttpServletRequest request) {
        model.addAttribute("message", "用户名或密码错误");
        return "../../Chair/ChairLogin";
    }

    @RequestMapping(value="/Minister/login")
    public String MinisterLogin(String accounts,String password,Model model,HttpServletRequest request) {

        users user = userservice.login(accounts, password);
        if(user==null) {
            return "redirect:ToMinisterLogin";
        }
        if(user.getPermission()=='2') {
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(3600);
            session.setAttribute("user", user);
            System.out.println("chuanrusession");
            Member member = memberService.getMemberByNO(user.getUserid());
            int depId = member.getDepartment();
            String depName = departmentService.getNameByID(depId);
            member.setDepartmentNa(depName);

            int coID = member.getCollegeId();
            String coName = collegeService.getNameByID(coID+"");
            member.setCollegeNa(coName);

            session.setAttribute("member", member);
            return "Minister/main";
        }else {
            return "redirect:ToMinisterLogin";
        }
    }
    @RequestMapping(value="/Minister/ToMinisterLogin")
    public String ToMinisterLogin(Model model,HttpServletRequest request) {
        model.addAttribute("message", "用户名或密码错误");
        return "../../Minister/MinisterLogin";
    }


    @RequestMapping(value="/Member/login")
    public String MemberLogin(String accounts,String password,Model model,HttpServletRequest request) {

        users user = userservice.login(accounts, password);
        if(user==null) {
            return "redirect:ToMemberLogin";
        }
        if(user.getPermission()=='1') {
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(3600);
            session.setAttribute("user", user);

            Member member = memberService.getMemberByNO(user.getUserid());
            int depId = member.getDepartment();
            String depName = departmentService.getNameByID(depId);
            member.setDepartmentNa(depName);

            int coID = member.getCollegeId();
            String coName = collegeService.getNameByID(coID+"");
            member.setCollegeNa(coName);

            session.setAttribute("member", member);
            return "Member/main";
        }else {
            return "redirect:ToMemberLogin";
        }
    }
    @RequestMapping(value="/Member/ToMemberLogin")
    public String ToMemberLogin(Model model,HttpServletRequest request) {
        model.addAttribute("message", "用户名或密码错误");
        return "../../Member/MemberLogin";
    }




    @RequestMapping(value="/quit")
    public String quitSystem(Model model,HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:quitToIndex";
    }

    @RequestMapping(value="/quitToIndex")
    public String quitToIndex(Model model,HttpServletRequest request) {
        return "../../index";
    }
    @RequestMapping(value="/modifyPassword")
    public String modifyPassword(Model model,String accounts,String newPassword1,String oldPassword,String newPassword2,Integer permission) {
        if(!(newPassword1.equals(newPassword2)))
        {
            model.addAttribute("message","两次密码不相同");
            if(permission == 3)
            return "Chair/ChairModifyPassword";
            if(permission == 2)
                return "Minister/MinisterModifyPassword";
            if(permission == 1)
                return "Member/MemberModifyPassword";
        }

        users user =userservice.queryuserById(accounts);
        String oldp =user.getPassword();

        if(!(oldp.equals(oldPassword)))
        {
            model.addAttribute("message","原密码输入错误");
            if(permission == 3)
            return "Chair/ChairModifyPassword";
            if(permission == 2)
                return "Minister/MinisterModifyPassword";
            if(permission == 1)
                return "Member/MemberModifyPassword";
        }

        int num = userservice.modifyPassword(accounts,newPassword1);
        model.addAttribute("num", num);

        return "modifySuccess";
    }

}
